﻿using System;
using fastCSharp.net.tcp.http;
using fastCSharp;
using fastCSharp.code.cSharp;
using System.IO;

namespace diantou.web
{
    /// <summary>
    /// web视图服务
    /// </summary>
    public partial class webServer
    {
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="domains"></param>
        /// <param name="onStop"></param>
        /// <returns></returns>
        public override bool Start(domain[] domains, Action onStop)
        {
            if (base.Start(domains, onStop))
            {
                fastCSharp.threading.threadPool.TinyPool.Start(loadSqlClientCache);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 是否已经加载数据库客户端缓存
        /// </summary>
        private bool isSqlClientCache;
        /// <summary>
        /// 数据库客户端缓存初始化
        /// </summary>
        private void loadSqlClientCache()
        {
            if (!isSqlClientCache)
            {
                string cachePath = fastCSharp.config.pub.Default.CachePath + @"sqlStreamCache\";
                #region 大字段缓存
                diantou.dataAccess.clientCache.Contest = new fastCSharp.sql.logStream<dataAccess.contest, dataModel.contest>.identityClient(diantou.dataAccess.tcpCall.contest.getSqlCache, diantou.dataAccess.tcpCall.contest.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.contest.Get, cachePath, 16);
                diantou.examDataAccess.clientCache.Exam = new fastCSharp.sql.logStream<examDataAccess.exam, dataModel.exam>.identityClient(diantou.examDataAccess.tcpCall.exam.getSqlCache, diantou.examDataAccess.tcpCall.exam.onSqlLog, value => value.Id, diantou.examDataAccess.tcpCall.exam.Get, cachePath, 16);
                diantou.examDataAccess.clientCache.ExamProblem = new fastCSharp.sql.logStream<examDataAccess.examProblem, dataModel.examProblem>.identityClient(diantou.examDataAccess.tcpCall.examProblem.getSqlCache, diantou.examDataAccess.tcpCall.examProblem.onSqlLog, value => value.Id, diantou.examDataAccess.tcpCall.examProblem.Get, cachePath, 16);
                diantou.examDataAccess.clientCache.ExamQuestion = new fastCSharp.sql.logStream<examDataAccess.examQuestion, dataModel.examQuestion>.identityClient(diantou.examDataAccess.tcpCall.examQuestion.getSqlCache, diantou.examDataAccess.tcpCall.examQuestion.onSqlLog, value => value.Id, diantou.examDataAccess.tcpCall.examQuestion.Get, cachePath);
                diantou.dataAccess.clientCache.Problem = new fastCSharp.sql.logStream<dataAccess.problem, dataModel.problem>.identityClient(diantou.dataAccess.tcpCall.problem.getSqlCache, diantou.dataAccess.tcpCall.problem.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.problem.Get, cachePath, 128);
                #endregion

                #region 常用数据
                diantou.dataAccess.clientCache.ContestOwner = new fastCSharp.sql.logStream<dataAccess.contestOwner, dataModel.contestOwner>.dictionaryClient<dataModel.contestOwner.primaryKey, bool>(diantou.dataAccess.tcpCall.contestOwner.getSqlCache, diantou.dataAccess.tcpCall.contestOwner.onSqlLog, value => new dataModel.contestOwner.primaryKey { ContestId = value.ContestId, UserId = value.UserId }, diantou.dataAccess.tcpCall.contestOwner.get, value => true, value => new dataAccess.contestOwner { ContestId = value.Key.Key.ContestId, UserId = value.Key.Key.UserId }, false, cachePath, "contestOwner", false);
                diantou.dataAccess.clientCache.ProblemGroup = new fastCSharp.sql.logStream<dataAccess.problemGroup, dataModel.problemGroup>.identityClient(diantou.dataAccess.tcpCall.problemGroup.getSqlCache, diantou.dataAccess.tcpCall.problemGroup.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.problemGroup.Get, cachePath, 16);
                diantou.dataAccess.clientCache.ContestProblem = new fastCSharp.sql.logStream<dataAccess.contestProblem, dataModel.contestProblem>.dictionaryClient<dataModel.contestProblem.primaryKey, int>(diantou.dataAccess.tcpCall.contestProblem.getSqlCache, diantou.dataAccess.tcpCall.contestProblem.onSqlLog, value => new dataModel.contestProblem.primaryKey { ContestId = value.ContestId, ProblemId = value.ProblemId }, diantou.dataAccess.tcpCall.contestProblem.get, value => value.Point, value => new dataAccess.contestProblem { ContestId = value.Key.Key.ContestId, ProblemId = value.Key.Key.ProblemId, Point = value.Value }, -1, cachePath, "contestProblem_Point", false);
                diantou.dataAccess.clientCache.ContestRegister = new fastCSharp.sql.logStream<dataAccess.focusContest, dataModel.focus>.dictionaryClient<dataModel.focus.primaryKey, DateTime>(diantou.dataAccess.tcpCall.focusContest.getSqlCache, diantou.dataAccess.tcpCall.focusContest.onSqlLog, value => new dataModel.focus.primaryKey { UserId = value.UserId, FocusId = value.FocusId }, diantou.dataAccess.tcpCall.focusContest.get, value => value.FocusTime, value => new dataAccess.focusContest { UserId = value.Key.Key.UserId, FocusId = value.Key.Key.FocusId, FocusTime = value.Value }, DateTime.MinValue, cachePath, "focusContest_Register", false, contest.contestRegister.Default);
                diantou.dataAccess.clientCache.Question = new fastCSharp.sql.logStream<dataAccess.question, dataModel.question>.identityClient(diantou.dataAccess.tcpCall.question.getSqlCache, diantou.dataAccess.tcpCall.question.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.question.Get, cachePath);
                diantou.dataAccess.clientCache.User = new fastCSharp.sql.logStream<dataAccess.user, dataModel.user>.identityClient(diantou.dataAccess.tcpCall.user.getSqlCache, diantou.dataAccess.tcpCall.user.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.user.Get, cachePath);
                diantou.examDataAccess.clientCache.Enterprise = new fastCSharp.sql.logStream<examDataAccess.enterprise, dataModel.enterprise>.identityClient(diantou.examDataAccess.tcpCall.enterprise.getSqlCache, diantou.examDataAccess.tcpCall.enterprise.onSqlLog, value => value.Id, diantou.examDataAccess.tcpCall.enterprise.Get, cachePath);
                #endregion

                diantou.dataAccess.clientCache.Medal = new fastCSharp.sql.logStream<dataAccess.medal, dataModel.medal>.identityClient(diantou.dataAccess.tcpCall.medal.getSqlCache, diantou.dataAccess.tcpCall.medal.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.medal.Get, cachePath, 16);
                diantou.dataAccess.clientCache.Tutorial = new fastCSharp.sql.logStream<dataAccess.tutorial, dataModel.tutorial>.identityClient(diantou.dataAccess.tcpCall.tutorial.getSqlCache, diantou.dataAccess.tcpCall.tutorial.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.tutorial.Get, cachePath, 16);
                diantou.dataAccess.clientCache.Favorite = new fastCSharp.sql.logStream<dataAccess.favorite, dataModel.favorite>.identityClient(diantou.dataAccess.tcpCall.favorite.getSqlCache, diantou.dataAccess.tcpCall.favorite.onSqlLog, value => value.Id, diantou.dataAccess.tcpCall.favorite.Get, cachePath);
                isSqlClientCache = true;

                diantou.examDataAccess.clientCache.ExamUser = new fastCSharp.sql.logStream<examDataAccess.examUser, dataModel.examUser>.queueClient<int>(diantou.examDataAccess.tcpCall.examUser.onSqlLog, value => value.Id, diantou.examDataAccess.tcpCall.examUser.Get, 1 << 16);
            }
        }
        /// <summary>
        /// 停止监听
        /// </summary>
        protected override void stopListen()
        {
            if (isSqlClientCache)
            {
                isSqlClientCache = false;
                diantou.dataAccess.clientCache.Save();
                diantou.examDataAccess.clientCache.Save();
            }
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="extensionName">文件名称信息</param>
        /// <returns>文件内容</returns>
        protected override subArray<byte> readCacheFile(subString extensionName)
        {
            subArray<byte> data = webConfig.ReadCacheFile(ref extensionName, WorkPath.Length, WebConfig.IsFileCacheHeader);
            return data.Count == 0 ? base.readCacheFile(extensionName) : data;
        }
    }
}
